<template>
  <div id="app">
    <el-container>
      <el-header height="50px">
        <div class="header">
          <h2>关系链可视化系统</h2>
          <div class="user-menu" v-if="isLoggedIn">
            <el-dropdown @command="handleUserCommand">
              <span class="user-info">
                <el-avatar :size="30" :src="userInfo.avatar" icon="el-icon-user-solid"></el-avatar>
                <span class="username">{{ userInfo.username || userInfo.full_name }}</span>
                <i class="el-icon-arrow-down"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="profile">个人资料</el-dropdown-item>
                <el-dropdown-item command="changePassword">修改密码</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { authAPI } from '@/api'

export default {
  name: 'App',
  data() {
    return {
      userInfo: {}
    }
  },
  computed: {
    isLoggedIn() {
      return !!localStorage.getItem('auth_token')
    }
  },
  mounted() {
    this.loadUserInfo()
  },
  methods: {
    loadUserInfo() {
      const userStr = localStorage.getItem('user_info')
      if (userStr) {
        try {
          this.userInfo = JSON.parse(userStr)
        } catch (error) {
          console.error('Failed to parse user info:', error)
        }
      }
    },

    handleUserCommand(command) {
      switch (command) {
        case 'profile':
          this.showProfile()
          break
        case 'changePassword':
          this.showChangePassword()
          break
        case 'logout':
          this.logout()
          break
      }
    },

    showProfile() {
      this.$message.info('个人资料功能开发中...')
    },

    showChangePassword() {
      this.$message.info('修改密码功能开发中...')
    },

    logout() {
      this.$confirm('确定要退出登录吗？', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        authAPI.logout()
        this.$message.success('已退出登录')
      })
    }
  },
  watch: {
    '$route'() {
      this.loadUserInfo()
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  height: 100vh;
  margin: 0;
  padding: 0;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: white;
  padding: 0 20px;
  height: 100%;
  width: 100%;
}

.header h2 {
  margin: 0;
  font-size: 18px;
}

.user-menu {
  color: white;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.username {
  margin: 0 8px;
  font-size: 14px;
}

.el-main {
  padding: 0 !important;
  margin: 0 !important;
  height: calc(100vh - 50px);
}

body {
  margin: 0;
  padding: 0;
}

.el-container {
  height: 100vh;
  margin: 0;
  padding: 0;
}

.el-header {
  background-color: #409EFF;
  padding: 0;
  margin: 0;
  border: none;
  display: flex;
  align-items: center;
}
</style>